package com.czkt.controller;

import com.czkt.pojo.Order;
import com.czkt.service.OrderService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ProducerController {

    @Resource
    RabbitTemplate rabbitTemplate;

//    @RequestMapping("/dispatcher")
//    public Object dispatcher(String email,String account) {
//        Map<String, Object> resultMap = new HashMap<>();
//        resultMap.put("email", email);
//        resultMap.put("account", account);
//        ObjectMapper objectMapper = new ObjectMapper();
//        try {
//            for (int i = 0; i < 100; i++) {
//                ByteArrayOutputStream bao = new ByteArrayOutputStream();
//                ObjectOutputStream oos = new ObjectOutputStream(bao);
//
//                // 将对象写入到输出流中
//                oos.writeObject(resultMap);
//
//                // 将输出流转换为字节数组
//                byte[] bytes = bao.toByteArray();
//                rabbitTemplate.send("queue", new Message(bytes));
//                // 关闭流
//                oos.close();
//                bao.close();
//            }
//
//
//            // 现在 bytes 包含了序列化后的对象
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        return resultMap;
//    }

//    @RequestMapping("/sendMessage")
//    public Object sendMessage(String message){
//        rabbitTemplate.convertAndSend("activityInfo","vip",message);
//        return "发送消息" + message + "成功!";
//    }

//    @RequestMapping("/sendMessage")
//    public Object sendMessage(String message){
//        rabbitTemplate.convertAndSend("activityInfo","ordinary",message);
//        return "发送消息" + message + "成功!";
//    }

//    @RequestMapping("/sendMessage")
//    public Object sendMessage(String message){
//        rabbitTemplate.convertAndSend("fanout.exchange",null,message);
//        return "发送消息" + message + "成功!";
//    }

//    @RequestMapping("/sendMessage")
//    public Object sendMessage(String message){
//        rabbitTemplate.convertAndSend("direct.exchange","vip",message);
//        rabbitTemplate.convertAndSend("direct.exchange","ordinary",message);
//        return "发送消息" + message + "成功!";
//    }

//    @RequestMapping("/sendMessage")
//    public Object sendMessage(String message){
//        rabbitTemplate.convertAndSend("topic.exchange","vip.coupon","优惠卷消息" + message);
//        rabbitTemplate.convertAndSend("topic.exchange","vip.activity.1","活动1消息" + message);
//        rabbitTemplate.convertAndSend("topic.exchange","vip.activity.2","活动2消息" + message);
//        rabbitTemplate.convertAndSend("topic.exchange","ordinary.coupon","优惠卷消息" + message);
//        rabbitTemplate.convertAndSend("topic.exchange","ordinary.activity.1","活动1消息" + message);
//        return "发送消息" + message + "成功!";
//    }

//    @RequestMapping("/sendMessage")
//    public Object sendMessage(String message){
//        rabbitTemplate.convertAndSend("amq.topic","aaaaaaaaa",message);
//        return "发送消息" + message + "成功!";
//    }

    @Resource
    OrderService orderService;

    @RequestMapping("/sendMessage")
    public String orderTest(Integer orderId){
        if(orderService.selectOrder(orderId) > 0){
            if(orderService.updateOrder(orderId)){
                return "下单成功";
            }
        }
        return "下单失败";
    }
}
